<?php

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class HT_Model_administrator_models_seocomment extends Zend_Db_Table {//ten class fai viet hoa

	protected $_db;

	public function __construct() {
		$this->_name = "seo_comment";
		$this->_db = Zend_Registry::get('dbMain');
		parent::init();
	}
	
	public function getSeoComments($limit){
		$ids = $this->getCommentIds($limit);
		$sql = " SELECT * FROM seo_comment WHERE comment_id IN (".implode(',', $ids).")";
		return $this->_db->fetchAll($sql);
	}
	
		
	private function getCommentIds($limit){
		$ids = array();
		$commentIds = array();
		$sql = " SELECT comment_id FROM seo_comment";
		$idList = $this->_db->fetchAll($sql);
		foreach((array)$idList as $idData){
			$ids[] = $idData['comment_id'];
		}
		
		while(sizeof($commentIds) < $limit){
			$index = rand(0,sizeof($ids)-1);
			if(!in_array($index,$commentIds)){
				$commentIds[] = $index;
			}
		}
		return $commentIds;
	}
	
	public function updateData($data,$seocommentId){
		$seocommentUrl = trim($data['comment']);
		if($seocommentUrl != '' && !$this->_checkExistsSeocommentUrl($seocommentUrl,'update',$seocommentId)){
			$this->update($data, "comment_id=".(int)$seocommentId);
			return $seocommentId;
		}else{
			return "-1";
		}
		
	}
	
	public function addData($data){
		$seocommentUrl = trim($data['comment']);
		if($seocommentUrl != '' && !$this->_checkExistsSeocommentUrl($seocommentUrl)){
			$this->insert($data);
			return $this->getMaxId();
		}else{
			return "-1";
		}
	}
	
	private function _checkExistsSeocommentUrl($seocommentUrl,$action="insert",$seocommentId = null){
		if($action == 'insert'){
			$sql = "SELECT COUNT(comment_id) FROM seo_comment WHERE comment = ?";
			return  (int)$this->_db->fetchOne($sql,array($seocommentUrl));
		}else{
			$sql = "SELECT COUNT(comment_id) FROM seo_comment WHERE comment = ? AND comment_id <> ?";
			return  (int)$this->_db->fetchOne($sql,array($seocommentUrl,$seocommentId));
		}
	}
	
	public function getMaxId(){
		$sql = "SELECT MAX(comment_id) FROM seo_comment";
		return  (int)$this->_db->fetchOne($sql);
	}
	public function getSeocomment($seocommentId,$filter = array()) {
		$sql = " SELECT * FROM seo_comment WHERE comment_id= ".(int)$seocommentId;
		return $this->_db->fetchRow($sql);
	}
	
	public function getSeocommentByUrl($url) {
		$sql = " SELECT * FROM seo_comment WHERE comment = ? LIMIT 1 ";
		return $this->_db->fetchRow($sql,array($url));
	}
	
	public function getListSeocomment_nb($filter = array()) {
		$sqlPlus = $this->getListSeocomment_sqlPlus($filter);
		$sql = "SELECT COUNT(comment_id)
		FROM seo_comment
		WHERE 1=1 $sqlPlus";
		return $this->_db->fetchOne($sql);
	}
	public function getListSeocomment($start=0,$size = 10,$filter = array()) {
		$sqlPlus = $this->getListSeocomment_sqlPlus($filter);
		$sql = "SELECT *
		FROM seo_comment
		WHERE 1=1 $sqlPlus ORDER BY comment LIMIT $start,$size";
		return $this->_db->fetchAll($sql);
	}
	
	private function getListSeocomment_sqlPlus($filter){
		$sqlPlus = null;
		$keyword = trim(@$filter['keyword']);
		if($keyword){
			$sqlPlus .= " AND (comment LIKE '%$keyword%') ";
		}
		return $sqlPlus;
	}
	
}

?>
